import MySQLdb

class Usuario:
    
    def __init__(self,Login="",Senha="",Privilegio="",Nome=""):

        self.IdUsuario = ""
        self.Login = Login
        self.Senha = Senha
        self.Privilegio = Privilegio
        self.Nome = Nome

    def getIdUsuario(self):
        return self.IdUsuario

    def setIdUsuario(self,novoId):
        self.IdUsuario = novoId
        
    def getLogin(self):
        return self.Login

    def setLogin(self,novoLogin):
        self.Login = novoLogin


    def getSenha(self):
        return self.Senha

    def setSenha(self,novoSenha):
        self.Senha = novoSenha

    def getPrivilegio (self):
        return self.Privilegio

    def setPrivilegio (self,novoPrivilegio):
        self.Privilegio = novoPrivilegio
        
    def getNome(self):
        return self.Nome

    def setLogin(self,novoNome):
        self.Nome = novoNome


    def insert(self):
        db = MySQLdb.connect("localhost","root","","gtec" )
        cursor = db.cursor()
        cursor.execute("insert into usuario values(%s,%s,%s,%s,%s)",(self.IdUsuario,self.Login,self.Senha,self.Privilegio,self.Nome))
        db.commit()
        
    def update(self):
        db = MySQLdb.connect("localhost","root","","gtec" )
        cursor = db.cursor()
        cursor.execute("UPDATE usuario SET login = '"+self.Login+"',senha = '"+self.Senha+"',privilegio = '"+self.Privilegio+"',\
                        nome = '"+self.Nome+"' WHERE IdUsuario = '"+str(self.IdUsuario)+"'")
        db.commit()

    def remover(self):
        db = MySQLdb.connect("localhost","root","","gtec" )
        cursor = db.cursor()
        
        cursor.execute("DELETE FROM usuario WHERE IdUsuario = '"+str(self.Login)+"'")
        db.commit()
        
    def load(self):
        db = MySQLdb.connect("localhost","root","","gtec" )
        cursor = db.cursor()
                       
        cursor.execute("SELECT * FROM usuario WHERE IdUsuario = '"+str(self.IdUsuario)+"'")
        if(cursor.rowcount == 0):
            return False
        else:
            pesq = cursor.fetchall()
            print "\n|ID |"
            for coluna in pesq: print  "|",coluna[0],"|" ,"\n Nome: ",coluna[4], "\n Login: ",coluna[1], "\n Privilegio: ",coluna[3]
        db.commit()

    def exibirUsuarios():
        
        db = MySQLdb.connect("localhost","root","","gtec" )
        cursor = db.cursor()
        comando = """SELECT * FROM usuario """
        cursor.execute(comando)
        pesq = cursor.fetchall()
        print "|Usuarios|\n |ID| "
        for coluna in pesq:  print  "|",coluna[0], "|" , " - ",coluna[4]
        db.commit()
